﻿namespace SharePointConsoleApplication1
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;

    using com.stifel;
    using com.stifel.Extensions.SharePointExtensions;
    using com.stifel.GlobalDao;
    using com.stifel.ServerBuildMgmtDao;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Linq;
    using Microsoft.SharePoint.WebControls;

    class Program
    {
        #region Methods

        static void addDesignerGroup( SPWeb spWeb, string groupName )
        {
            using ( SPSite spSite = spWeb.Site )
            {
                SPGroup spGroup = spSite.AddSiteGroup( groupName, string.Format( "Use this group to grant people design permissions to the {0} site. Members of this group can view, add, update, delete, approve, and customize. Members of this group also has read permissions to all parent sites. They can view pages and list items and download documents on all parent sites.", spWeb.Title ) );
                bool ok = true;
                SPWeb parentWeb = spWeb;
                while ( ok )
                {
                    using ( SPWeb o = parentWeb.ParentWeb )
                    {
                        if ( o == null )
                        {
                            ok = false;
                            break;
                        }
                        else
                        {
                            o.AddGroupWithRole( spGroup, SPRoleType.Reader );
                            parentWeb = o;
                        }
                    }
                }
                spWeb.AddGroupWithRole( spGroup, SPRoleType.WebDesigner );
            }
        }

        static void Main( string[ ] args )
        {


            //CustomLoggingService.LogMessage( CustomLoggingService.Area.FeatureInfrastructure , "The time has come the Walrus said to speak of many things.");

            //return;
            string CurrentWebUrl = "http://win-pv542842fga:2210/sites/stifelcoresystems/virtualserverbuildrqs/";

            string name = "usr_tz_lookupvalue";
            using ( SPSite spSite = new SPSite( CurrentWebUrl ) )
            {
                using ( SPWeb p = spSite.OpenWeb())// spSite.RootWeb )
                {
                    SPField field = null;
                    if ( p.Fields.ContainsFieldWithStaticName( name ) )
                        field = p.Fields.GetFieldByInternalName( name );
                    else
                    {
                        if ( p.Site.RootWeb.Fields.ContainsFieldWithStaticName( name ) )
                            field = p.Site.RootWeb.Fields.GetFieldByInternalName( name );
                    }
                    if ( field != null )
                    {
                    }



                    bool yes = p.Webs.ContainsSubweb( "t2" );
                    using ( SPWeb q = p.GetWeb( "t1" ) )
                    using ( SPWeb r = q.GetWeb( "t2" ) )
                    {
                        addDesignerGroup( r, "Build.Designer" );
                    }
                }
                //SPGroup spGroup = spSite.GetSiteGroup( "test1" );

                using ( SPWeb spWeb = spSite.RootWeb )
                {
                    //spWeb.RemoveGroup( "test1" );
                    //var groups = spWeb.GetSiteGroupsWithRole( SPRoleType.Reader );
                    //spWeb.AddGroupWithRole( spGroup, SPRoleType.Reader );
                    using ( SPWeb q = spWeb.GetWeb( "t1" ) )
                    {
                        SPGroup spGroup = spSite.AddSiteGroup( "AV.Designer", "Use this group to grant people design permissions to the AD Team site. Members of this group can view, add, update, delete, approve, and customize. Members of this group also has read permissions to the root site. They can view pages and list items and download documents on the root site." );
                        spWeb.AddGroupWithRole( spGroup, SPRoleType.Reader );
                        q.AddGroupWithRole( spGroup, SPRoleType.WebDesigner );
                        spGroup = spSite.AddSiteGroup( "AV.Member", "Use this group to grant people contribute permissions to the AD Team site. Members of this group can view, add, update, and delete list items and documents. Members of this group also has read permissions to the root site. They can view pages and list items and download documents on the root site." );
                        spWeb.AddGroupWithRole( spGroup, SPRoleType.Reader );
                        q.AddGroupWithRole( spGroup, SPRoleType.Contributor );
                        spGroup = spSite.AddSiteGroup( "AV.Owner", "Use this group to grant people full control permissions to the AD Team site. Members of this group has full control. Members of this group also has read permissions to the root site. They can view pages and list items and download documents on the root site." );
                        spWeb.AddGroupWithRole( spGroup, SPRoleType.Reader );
                        q.AddGroupWithRole( spGroup, SPRoleType.Administrator );
                        spGroup = spSite.AddSiteGroup( "AV.Viewer", "Use this group to grant people view only permissions to the AD Team site. Members of this group can view pages, list items, and documents. Document tyeps with server-side file handlers can be viewed in the browser but not downloaded. Members of this group also has read permissions to the root site. They can view pages and list items and download documents on the root site." );
                        spWeb.AddGroupWithRole( spGroup, SPRoleType.Reader );
                        q.AddGroupWithRole( spGroup, SPRoleType.Reader );
                        spGroup = spSite.AddSiteGroup( "AV.Visitor", "Use this group to grant people read permissions to the AD Team site. Members of this group can view pages and list items and download documents. Members of this group also has read permissions to the root site. They can view pages and list items and download documents on the root site." );
                        spWeb.AddGroupWithRole( spGroup, SPRoleType.Reader );
                        q.AddGroupWithRole( spGroup, SPRoleType.Reader );

                    }
                }
                using ( SPWeb spWeb = spSite.RootWeb )
                {
                    SPUser user = spWeb.SiteUsers.Cast<SPUser>( ).ToList( ).FirstOrDefault( a => a.IsSiteAdmin );
                    spWeb.SiteGroups.Add( "test1", user, user, "This is a test security group" );
                    SPGroup group = spWeb.SiteGroups[ "test1" ];
                    SPRoleAssignment ra = new SPRoleAssignment( group );
                    SPRoleDefinition rd = spWeb.RoleDefinitions.GetByType( SPRoleType.Reader );
                    ra.RoleDefinitionBindings.Add( rd );
                    spWeb.RoleAssignments.Add( ra );
                    spWeb.Update( );
                    using ( SPWeb t1 = spWeb.GetWeb( "t1" ) )
                    {
                        //t1.AllowUnsafeUpdates = true;
                        t1.Update( );
                        t1.BreakRoleInheritance( true );
                        SPRoleAssignment t1ra = new SPRoleAssignment( group );
                        SPRoleDefinition t1rd = t1.RoleDefinitions.GetByType( SPRoleType.Administrator );
                        t1ra.RoleDefinitionBindings.Add( t1rd );
                        t1.RoleAssignments.Add( t1ra );
                        t1.Update( );
                    }
                }
            }

            TimezoneDao tzdao = new TimezoneDao( CurrentWebUrl );
            var v = tzdao.Get( TimeZoneInfoConstants.EasternStandardTime );

            using ( SPSite spSite = new SPSite( CurrentWebUrl ) )
            {
                using ( SPWeb spWeb = spSite.OpenWeb( ) )
                {
                    string url = spWeb.Url;
                }
            }
            //            SPControl c = new SPControl( );

            //StifelVirtualServerLocationDao dao = new StifelVirtualServerLocationDao( "http://win-pv542842fga:2210/sites/test/t1" );
            USStatesDao usDao = new USStatesDao( "http://win-pv542842fga:2210/sites/test/t1" );
            USStates usst = usDao.Get( "CA" );

            StifelVirtualServerLocationDao vsdao = new StifelVirtualServerLocationDao( "http://win-pv542842fga:2210/sites/test/t1" );

            //dao.Add( new StifelVirtualServerLocation
            //{
            //    TimeZoneLookupId = 1,
            //    USStatesLookupId = usst.Id,
            //    Title = "Testing_" + usst.Title
            //} );
        }

        #endregion Methods

        #region Nested Types

        class abc
        {
            #region Properties

            public List<DayOfWeek> DayHours
            {
                get;
                set;
            }

            public List<DayOfWeek> NightHours
            {
                get;
                set;
            }

            #endregion Properties
        }

        #endregion Nested Types
    }
}